<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计划管理</title>

    <script src="../../plugs/jquery-3.1.1.min.js"></script>
    <link rel="stylesheet" href="../../assets/bootstrap/css/bootstrap.min.css">
    <script src="../../assets/bootstrap/js/bootstrap.min.js"></script>
    <!--富文本编辑器-->
    <link href="../../assets/summernote/summernote.css" rel="stylesheet">
    <script src="../../assets/summernote/summernote.js"></script>
    <!--jquery-ui-->
    <link href="https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.js"></script>
    <link rel="stylesheet" href="../css/platformManager.css">
    <link rel="stylesheet" href="../../css/layer/mobile/need/layer.css">
    <script type="text/javascript" src="../../css/layer/layer.js"></script>
    <style>
    	.changeFormStyle{
			border-top: 1px solid #ccc;
			padding: 5px 0;
			margin-top: 10px;
		}	
    </style>
</head>
<body>
<div class="container" style="margin-top:20px;width: 1200px">
    <form class="form-horizontal">
        <fieldset>
            <legend>新建计划</legend>
            <div class="control-group" >
<!--                <div class="controls" >-->
<!--                    <label class="control-label" for="planName">计划名称</label>-->
<!--                    <input id="planName" name="planName" type="text" placeholder="" class="input-xlarge" required="required">-->
<!--                    <span class="help-block"></span>-->
<!--                </div>-->
                <div class="input-group">
                    <span class="input-group-addon" id="sizing-addon1">计划名称</span>
                    <input style="width: 500px" id="planName" name="planName" type="text" class="form-control" placeholder="计划名称...." aria-describedby="sizing-addon1">
                </div>
            </div>

			 <div class="control-group" >
<!--                <div class="controls" >-->
<!--                    <label class="control-label" for="cropCode">作物编码</label>-->
<!--                    <input id="cropCode" name="cropCode" type="text" placeholder="" class="input-xlarge" required="required">-->
<!--                    <span class="help-block">2位数字</span>-->
<!--                </div>-->
                 <div class="input-group">
                     <span class="input-group-addon" id="sizing-addon2">冷链编码</span>
                     <input style="width: 500px" id="cropCode" name="cropCode" type="text" class="form-control" placeholder="请输入两位数字的冷链编码...." aria-describedby="sizing-addon1">
                 </div>
            </div>


            <div class="control-group" >
<!--                <div class="controls" >-->
<!--                    <label class="control-label" for="productName">商品名称</label>-->
<!--                    <input id="productName" name="productName" type="text" placeholder="" class="input-xlarge" required="required">-->
<!--                    <span class="help-block"></span>-->
<!--                </div>-->
                <div class="input-group">
                    <span class="input-group-addon" id="sizing-addon3">商品名称</span>
                    <input style="width: 500px" id="productName" name="productName" type="text" class="form-control" placeholder="商品名称...." aria-describedby="sizing-addon1">
                </div>
            </div>

            <div class="control-group" >
<!--                <div class="controls" >-->
<!--                    <label class="control-label" for="businessID">年度信息</label>-->
<!--                    <input id="year" name="year" type="number" placeholder="" class="input-xlarge" required="required">-->
<!--                    <span class="help-block"></span>-->
<!--                </div>-->
                <div class="input-group">
                    <span class="input-group-addon" id="sizing-addon4">年度信息</span>
                    <input style="width: 500px" id="year" name="year" type="number" class="form-control" placeholder="年度信息...." aria-describedby="sizing-addon1">
                </div>
            </div>

            <div class="control-group plan changeFormStyle" >
                <div class="controls" >
                    <label class="control-label" for="planTemplate">选择计划模板</label>
                     <div class="controls jscheckbox" id="planId">
                		<!--js动态添加-->
          			 </div>
                </div>
            </div>
            <!-- 动态 -->
        	<div id="box">
        		<p style="color:red;margin-top: 20px">(提示:指定计划参与人员：必须包含有来源记录，加工，入库，包装、运输、批发、销售各个环节)</p>
        	</div>


      		<!-- Textarea -->
            <div class="control-group changeFormStyle">
                <div class="controls">
                    <label class="control-label" for="Comment">备注</label>
                    <div id="Comment"><p>Hello Summernote</p></div>
                    <script>
                        $(document).ready(function() {
                            $('#Comment').summernote({
                                height:200
                            });
                        });
                    </script>
                </div>

            </div>

            <div class="control-group">
                <button type="button" id="submit" name="submit" class="btn btn-info">提交</button>
            </div>

        </fieldset>
    </form>
</div>
<script>
$(document).ready(function(){
	var businessId = $(window.parent.parent.frames["topFrame"].document).find('#businessId').text();
	//获取平台的生产模板信息
	$.ajax({
		type: "post",
		url: "../../system/getProductPlans.do",
		success: function(data)
		{
			fillPlan(data);
		},
		error: function(status)
		{
            layer.msg(status,{icon: 5})
		}	
	});
	//获取企业下员工信息
	$.ajax({
		type: "post",
		url: "../getWorkers.do",
		data:{"businessId":businessId},
		success: function(data)
		{
			fillWorkers(data);
		},
		error: function(status)
		{
            layer.msg(status,{icon: 5})
		}	
	});
	//动态添加列表
	function fillPlan(data)
	{	
		var len = data.list.length;
		//alert(len);
		for(var i= 0 ; i < len ; i++)
		{
		 	var output="";
			output+="<label class='checkbox inline' for='permissions-"+i+"'>"+
            "<input type='radio' name='permissions' id='permissions' value='"+data.list[i].planId+"'>"+
            data.list[i].planName+
            //" 备注:"+data.list[i].planDesc+
            "</label>";
        	$("#planId").append(output);
		}
	}

	//动态添加列表
	function fillWorkers(data)
	{	
		var len = data.list.length;
		var roleIds01 = new Array();
		var roleNames01 = new Array();
		for(var i = 0;i<len;i++)
		{
			var roleId = data.list[i].roleId;
			var rileName = data.list[i].roleName;
            roleIds01.push(roleId);
            roleNames01.push(rileName);
			
		}
        var roleIds = [];
        for (var i = 0; i < roleIds01.length; i++) {
            if (roleIds .indexOf(roleIds01[i]) === -1) {
                roleIds .push(roleIds01[i])
            }
        }
        var roleNames = [];
        for (var i = 0; i < roleNames01.length; i++) {
            if (roleNames .indexOf(roleNames01[i]) === -1) {
                roleNames .push(roleNames01[i])
            }
        }
        console.log(roleIds);
        console.log(roleNames)

		
		for(var i=0;i<roleIds.length;i++)
		{
			var output="<div class='control-group plan changeFormStyle' >"+
      	 	"<div class='controls' >"+
         	"<label class='control-label' for='"+roleIds[i]+"'>"+roleNames[i]+"</label>"+
            "<div class='controls jscheckbox' id='p"+roleIds[i]+"'>"+
      		"</div></div</div>";
      		$('#box').append(output);
		}
		
		for(var i= 0 ; i < len ; i++)
		{
            for(var j=0;j<roleIds.length;j++)
			{
				if(roleIds[j]==data.list[i].roleId)
				{
					var output="";
	    			output+="<label class='checkbox inline' for='"+roleIds[j]+"'>"+
	                "<input type='radio' name='"+roleIds[j]+"' id='role' value='"+data.list[i].workerId+"'>"+
	                data.list[i].workerName+
	                "</label>";
	          	 	 $("#p"+roleIds[j]).append(output);
				}
			}
		}
	}
	//验证表单数据，获取表单内ring没处理数据，提交后台
	$('#submit').click(function()
	{
		var businessId = $(window.parent.parent.frames["topFrame"].document).find('#businessId').text();
		var planName = $('#planName').val();
		var cropCode = $('#cropCode').val();
		var productName = $('#productName').val();
		var year = $('#year').val();
		//var roleDesc = $('#Comment').text();
		var planId = $('#planId').find('#permissions:checked').val();
		
		
		/* var sow = $('#sow').find('#sow:checked').val();
		var check = $('#check').find('#check:checked').val();
		var copack = $('#copack').find('#copack:checked').val();
		var deliver = $('#deliver').find('#deliver:checked').val(); */
		
		var workerIds = "";
		$('#box').find('input:radio:checked').each(function()
		{
			workerIds+=":"+$(this).val();
		});
		var comment =  $('#Comment').summernote('code');
		/*
			对数据进行验证
		*/
		
		if(businessId==""||planName==""||cropCode==""||productName==""||year==""||planId==""||
				workerIds==""||comment=="")
		{
            layer.msg("表单信息不完整",{icon: 5})
		}
		else
		{
			//提交哦数据
			$.ajax({
				type: 'post',
				url: '../addBusinessProjectPlan.do',
				data: {
					"planName":planName,
					"cropCode":cropCode,
					"productName":productName,
					"planId":planId,
					"year":year,				
	/* 				"growerNum":sow,
					"checkedNum":check,
					"packingClerkNum":copack,
					"logiticsNum":deliver, */
					"projectDesc": comment,
					"business":businessId,
					"workerIds":workerIds
				},
				success: function(data,status)
				{
					if(data=="true")
                        layer.msg("提交成功",{icon: 1})
					if(data=="false")
                        layer.msg("提交失败",{icon: 5})
					if(data=="loss")
                        layer.msg("人员权限不能覆盖溯源所需要的环节，请重新选择！",{icon: 5})
				},
				error: function(status)
				{
                    layer.msg("服务器内部错误",{icon: 5})
				}
			});	
			
		}
		
	
	});	
});
</script>
</body>
</html>